package cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.button;

import cn.schoolwow.ams.domain.block.common.operation.CustomerMessage;
import cn.schoolwow.ams.domain.block.common.operation.ListBlockHandlerBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockOperationHandler;
import cn.schoolwow.ams.domain.block.list.common.ShowButtonOption;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockOperationPosition;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.business.EnableScheduleJobFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class AddEnableScheduleJobButtonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);

        ListBlockOperationHandler blockOperationHandler = new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);

                flowMonitorOption.flowMonitorQuickFlow.startFlow(new EnableScheduleJobFlow())
                        .putInstanceData(listBlockRequest)
                        .execute();
                return null;
            }
        };
        List<ShowButtonOption> showButtonOptionList = new ArrayList<>();
        showButtonOptionList.add(new ShowButtonOption("enable", false));
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation("启用", blockOperationHandler);
            blockOperation.showButtonOptionList.addAll(showButtonOptionList);
            entityTableListBlockOption.blockOperationList.add(blockOperation);
        }
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation("批量启用", blockOperationHandler);
            blockOperation.showButtonOptionList.addAll(showButtonOptionList);
            blockOperation.position = BlockOperationPosition.MultipleSelect;
            entityTableListBlockOption.blockOperationList.add(blockOperation);
        }
    }

    @Override
    public String name() {
        return "添加启用定时任务按钮";
    }
}
